import ArgumentType from './extension-support/argument-type';
import BlockType from './extension-support/block-type';
import blockIconURI from './image/icon.svg';

const url="http://openapi.tuling123.com/openapi/api/v2";

const requestData='{"reqType":0,"perception":{"inputText":{"text":""},"inputImage":{"url":"imageUrl"},"selfInfo":{"location":{"city":"","province":"","street":""}}},"userInfo":{"apiKey":"","userId":""}}';

const axios = require('axios')

class UL{


        constructor(runtime){
            this.runtime = runtime;
            this.resdata="你好"
            this.uid="123456"
            this.apikey="abcd"
            this.province="上海"
            this.city="上海"
            this.street="博云路"
            this.statuscode="0"
        }

        logprint(l){
            console.log(l+"#")
        }
        // 函数实现，参数单位 毫秒 ；
        async wait(ms) {
            return new Promise(resolve =>setTimeout(() =>resolve(), ms));
        };

 

        async posturl(text){

            console.log("-posturl-------");
            
            this.statuscode="0"
            await this.wait(50);

            const obj = JSON.parse(requestData);

            //console.log(obj.perception.inputText.text);
            // expected output: 42
            obj.perception.inputText.text=text;
            //console.log(obj.perception.inputText.text);

            obj.perception.selfInfo.location.city=this.city;
            obj.perception.selfInfo.location.province=this.province;
            obj.perception.selfInfo.location.street=this.street;

            obj.userInfo.apiKey=this.apikey;
            obj.userInfo.userId=this.uid;
            await this.wait(50);
            console.log(JSON.stringify(obj))


            axios
            .post('http://openapi.tuling123.com/openapi/api/v2', JSON.stringify(obj))
            .then(res => {
                    
                console.log(`状态码: ${res.status}`)
                
                console.log("-1--------");
                //console.log(res.request);
                //console.log("-2--------");
                //console.log(res.request.response);
                //console.log("-3--------");
                console.log(res.request.responseText);
                //console.log(typeof res.request.responseText);
                //console.log("-4--------");
                //console.log(res)
                this.resdata=res.request.responseText;
                this.statuscode=res.status
                console.log("end")
                

            })
            .catch(error => {
            console.error(error)
            })
            
            console.log("sleep")

            for (var i=0;i<100;i++)
            { 
                await this.wait(50);
                if(this.statuscode=="200"){
                    console.log("200 return")
                    await this.wait(50);
                    return
                }
            }


        }
}

class YourExtension{
    constructor(runtime) {
        this.runtime = runtime;
        this.ul = new UL(runtime);
    }

    getInfo () {
        return {
            name: 'tuling',
            blockIconURI: blockIconURI,
            color: '#0a83e8',
            blocks: [
                {  
                    opcode: 'init',
                    blockType: BlockType.COMMAND,
                    text: '图灵机器人 初始化 用户名[UID] apiKey[KEY] ',
                    arguments: {
                        UID: {
                            type: ArgumentType.STRING,
                            defaultValue: "123456"
                        },
                        KEY: {
                            type: ArgumentType.STRING,
                            defaultValue: "abcd"
                        }
                    }
                },
                {  
                    opcode: 'localtion',
                    blockType: BlockType.COMMAND,
                    text: '图灵机器人 设置位置  省: [PRO]  市: [CITY]  街道: [STR] ',
                    arguments: {
                        PRO: {
                            type: ArgumentType.STRING,
                            defaultValue: "上海"
                        },
                        CITY: {
                            type: ArgumentType.STRING,
                            defaultValue: "上海"
                        },
                        STR: {
                            type: ArgumentType.STRING,
                            defaultValue: "博云路"
                        }
                    }
                },
                {  
                    opcode: 'talk',
                    blockType: BlockType.REPORTER,
                    text: '图灵机器人 对话 [VAL] ',
                    arguments: {
                        VAL: {
                            type: ArgumentType.STRING,
                            defaultValue: "你好"
                        }
                    }
                }
            ],
            menus: {
                
            }
        }
    }

    // 函数实现，参数单位 毫秒 ；
    async wait(ms) {
        //return new Promise(async(resolve) =>setTimeout(() =>resolve(), ms));
    };

        // block运行函数, 与opcode同名
        init (args) {
            console.log(args)

            this.ul.uid=args.UID
            this.ul.apikey=args.KEY 

            return 
      }

        // block运行函数, 与opcode同名
        localtion (args) {
            this.ul.province=args.PRO
            this.ul.city=args.CITY
            this.ul.street=args.STR
            return 
        }

    // block运行函数, 与opcode同名
    async talk (args) {
        console.log("run calc")
        console.log(this.ul.resdata);
        await this.ul.posturl(args.VAL);
         //await this.wait(5000);
          //console.log("-5--------");
          //const dat={"intent":{"actionName":"","code":10037,"intentName":""},"results":[{"groupType":1,"resultType":"text","values":{"text":"等我以后去那了，我再告诉你吧~"}}]}
          //console.log(dat)
          //console.log(typeof dat)
          //console.log(rs)
          console.log("return")
          console.log(this.ul.resdata);

        //const dat='{"intent":{"actionName":"","code":10037,"intentName":""},"results":[{"groupType":1,"resultType":"text","values":{"text":"等我以后去那了，我再告诉你吧~"}}]}';

        const obj = JSON.parse(this.ul.resdata);
        console.log(obj.intent.code);
        // expected output: 42
        console.log(obj.results[0].values.text);
        // expected output: true
        return obj.results[0].values.text
    }
}
module.exports = YourExtension;